package beer.coder.imageviewer.handler;

import beer.coder.imageviewer.App;
import beer.coder.imageviewer.ImageViewerUtil;
import java.io.File;
import java.util.List;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.TransferMode;

/**
 * TODO
 *
 * @author amath0312
 * @version 1.0
 * @date 2020-11-01 16:10
 */
public class SceneDropHandler implements EventHandler<DragEvent> {

  App app;

  public SceneDropHandler(App app) {
    this.app = app;
  }


  @Override
  public void handle(DragEvent event) {
    if (event.getEventType() == DragEvent.DRAG_OVER) {
      if (event.getDragboard().hasFiles()) {
        event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
        event.consume();
      }
    } else if (event.getEventType() == DragEvent.DRAG_DROPPED) {
      if (event.getDragboard().hasFiles()) {
        List<File> files = event.getDragboard().getFiles();
        if (files != null && files.size() > 0) {
          File file = files.get(0);
          app.loadImageViewer(
              ImageViewerUtil.loadImage(file),
              ImageViewerUtil.nameOfImage(file.toString())
          );
        }
      }
    }
  }
}
